package edu.nf.relaxenjoy.service;

import edu.nf.relaxenjoy.common.util.JsonUtils;
import edu.nf.relaxenjoy.common.util.RedisUtils;
import edu.nf.relaxenjoy.domain.entity.Album;
import edu.nf.relaxenjoy.domain.entity.Comment;
import edu.nf.relaxenjoy.domain.entity.Post;
import edu.nf.relaxenjoy.domain.entity.User;
import edu.nf.relaxenjoy.domain.state.AlbumState;
import edu.nf.relaxenjoy.domain.state.CommentState;
import edu.nf.relaxenjoy.domain.state.PostState;
import edu.nf.relaxenjoy.domain.state.UserState;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 将数据进行缓存
 * @author zhu
 * @date 2024-12-16
 */
@Component
@RequiredArgsConstructor
public class PlatformDataToCache {

    private final RedisUtils redisUtils;

    public void albumDataToCache(Album album, String key, int value) {
        AlbumState albumState;
        if (Boolean.TRUE.equals(redisUtils.hasKey(key))) {
            albumState = JsonUtils.parseObject(redisUtils.get(key), AlbumState.class);
            albumState.setCollectionCount(albumState.getCollectionCount() + value);
        } else {
            albumState = new AlbumState();
            albumState.setAid(album.getId());
            albumState.setCollectionCount(album.getCollectionCount() + value);
        }
        redisUtils.set(key, JsonUtils.toJsonString(albumState));
    }

    public void userDataToCache(User user, String key, int type, int value) {
        UserState userState;
        if (Boolean.TRUE.equals(redisUtils.hasKey(key))) {
            userState = JsonUtils.parseObject(redisUtils.get(key), UserState.class);
            if (type == 0) {
                userState.setTrendCount(userState.getTrendCount() + value);
            } else if (type == 1) {
                userState.setFollowCount(userState.getFollowCount() + value);
            } else {
                userState.setFanCount(userState.getFanCount() + value);
            }
        } else {

            if (type == 0) {
                user.setTrendCount(user.getTrendCount() + value);
            } else if (type == 1) {
                user.setFollowCount(user.getFollowCount() + value);
            } else {
                user.setFanCount(user.getFanCount() + value);
            }
            userState = new UserState();
            userState.setUid(user.getId());
            userState.setTrendCount(user.getTrendCount());
            userState.setFollowCount(user.getFollowCount());
            userState.setFanCount(user.getFanCount());
        }
        redisUtils.set(key, JsonUtils.toJsonString(userState));
    }

    public void commentDataToCache(Comment comment, String key, int type, int value) {
        CommentState commentState;
        if (Boolean.TRUE.equals(redisUtils.hasKey(key))) {
            commentState = JsonUtils.parseObject(redisUtils.get(key), CommentState.class);
            if (type == 0) {
                commentState.setCount(commentState.getCount() + value);
            } else if (type == 1) {
                commentState.setTwoNums(commentState.getTwoNums() + value);
            }
        } else {
            if (type == 0) {
                comment.setCount(comment.getCount() + value);
            } else if (type == 1) {
                comment.setTwoNums(comment.getTwoNums() + value);
            }

            commentState = new CommentState();
            commentState.setCid(comment.getId());
            commentState.setCount(comment.getCount());
            commentState.setTwoNums(comment.getTwoNums());

        }
        redisUtils.set(key, JsonUtils.toJsonString(commentState));
    }

    public void postDataToCache(Post post, String key, int type, int value) {
        PostState postState;
        if (Boolean.TRUE.equals(redisUtils.hasKey(key))) {
            postState = JsonUtils.parseObject(redisUtils.get(key), PostState.class);
            if (type == 0) {
                postState.setAgreeCount(postState.getAgreeCount() + value);
            } else if (type == 1) {
                postState.setCollectionCount(postState.getCollectionCount() + value);
            } else if (type == 2) {
                postState.setCommentCount(postState.getCommentCount() + value);
            } else {
                postState.setViewCount(postState.getViewCount() + value);
            }
        } else {
            postState = new PostState();

            if (type == 0) {
                postState.setAgreeCount(post.getAgreeCount() + value);
            } else if (type == 1) {
                postState.setCollectionCount(post.getCollectionCount() + value);
            } else if (type == 2) {
                postState.setCommentCount(post.getCommentCount() + value);
            } else {
                postState.setViewCount(post.getViewCount() + value);
            }

            postState.setMid(post.getId());
            postState.setAgreeCount(post.getAgreeCount());
            postState.setCommentCount(post.getCommentCount());
            postState.setCollectionCount(post.getCollectionCount());
            postState.setViewCount(post.getViewCount());
        }
        redisUtils.set(key, JsonUtils.toJsonString(postState));
    }
}
